﻿//-----------------------------------------------------------------
//
// @(#)$Id: RealPostageDetail.aspx.cs,v 1.0 2013/12/13 17:38:01 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:38:01 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ebay.Dal.CenterDB_BasTableAdapters;
using ebay.Dal.CenterDB_LTableAdapters;

public partial class UI_Bas_RealPostageDetail : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        T_ExpressShippingMethodsTableAdapter adapter = new T_ExpressShippingMethodsTableAdapter();
        ddlExpressCompany.DataSource = adapter.GetData();
        ddlExpressCompany.DataTextField = "Express";
        ddlExpressCompany.DataValueField = "Id";
        ddlExpressCompany.DataBind();
        ddlExpressCompany.Items.Insert(0, new ListItem("《--请选择--》", "Select"));

        ddlStatus.Items.Insert(0, new ListItem("《--请选择--》", "Select"));
        ddlStatus.Items.Insert(1, new ListItem("未审核", "0"));
        ddlStatus.Items.Insert(2, new ListItem("已审核", "1"));

        //发货方式Delivery       
    }

    static string act = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        act = Request.QueryString["act"];
        if (!IsPostBack)
        {
            switch (act)
            {
                case "I":
                    {
                        ddlStatus.SelectedValue = "0";                       
                    }
                    break;
                case "U":
                    {
                        this.txtId.Attributes["readonly"] = "readonly";
                        string id = Request.QueryString["id"];
                        T_RealPostageTableAdapter adapter = new T_RealPostageTableAdapter();
                        DataTable dt = adapter.GetDataById(id);
                        DataRow r = dt.Rows[0];
                        if (dt.Rows.Count > 0)
                        {
                            foreach (Control ctr in form1.Controls)
                            {
                                if (ctr is HtmlInputText)
                                {
                                    HtmlInputText txtbox = (HtmlInputText)ctr;
                                    string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);
                                    txtbox.Value = r[name].ToString();
                                }
                                else if (ctr is HtmlSelect)
                                {
                                    HtmlSelect ddlbox = (HtmlSelect)ctr;
                                    string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                    ddlbox.Value = r[name].ToString();
                                }
                                else if (ctr is DropDownList)
                                {
                                    DropDownList ddlist = (DropDownList)ctr;
                                    string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);
                                    ddlist.Text = r[name].ToString();                                    
                                }
                            }
                        }                        
                    }
                    break;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DateTime defaultDateTime = new DateTime(1900, 1, 1);
        if (Session["UserId"] == null)
        {
            WebUtility.ShowMsg("登陆超时,请重新登陆", this);
            return;
        }
        string userId = Session["UserId"].ToString();

        if (!string.IsNullOrEmpty(act))
        {
            switch (act)
            {
                case "I":
                    {
                        T_RealPostageTableAdapter adatper = new T_RealPostageTableAdapter();
                        int flag = int.Parse(adatper.IsExists(txtOrderId.Value).ToString());
                        if (flag == 0)
                        {
                            if (ddlExpressCompany.SelectedValue == "2")
                            {
                                txtOrderId.Value = " ";
                            }
                            int val = adatper.Insert(
                                txtId.Value,
                                ddlExpressCompany.SelectedValue=="2"?" ":txtOrderId.Value,
                                Convert.ToDecimal(txtRealPostage.Value == "" ? "0" : txtRealPostage.Value),
                                ddlExpressCompany.SelectedValue,
                                0,
                                Convert.ToDateTime(dteDeliveryDate.Value),
                                txtMnote.Value,
                                userId,
                                System.DateTime.Now,
                                userId,
                                defaultDateTime
                                );
                            if (val > 0)
                            {
                                Response.Write("<script>alert('添加成功!');window.location.href='RealPostage.aspx'</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('此订单号已经存在!..');</script>");
                        }
                    }
                    break;
                case "U":
                    {
                        string id = Request.QueryString["id"];
                        T_RealPostageTableAdapter adapter = new T_RealPostageTableAdapter();
                        DataTable dt = adapter.GetDataById(id);
                        DataRow r = dt.Rows[0];
                        if (dt.Rows.Count > 0)
                        {
                            foreach (Control ctr in form1.Controls)
                            {
                                if (ctr is HtmlInputText)
                                {
                                    HtmlInputText txtbox = (HtmlInputText)ctr;
                                    string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);

                                    r[name] = txtbox.Value;
                                }
                                else if (ctr is HtmlSelect)
                                {
                                    HtmlSelect ddlist = (HtmlSelect)ctr;
                                    string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);
                                    r[name] = ddlist.Value;
                                }
                                else if (ctr is DropDownList)
                                {
                                    DropDownList ddlist = (DropDownList)ctr;
                                    string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);
                                    r[name] = ddlist.SelectedValue;
                                }
                                else if (ctr is TextBox)
                                {
                                    TextBox txtbox = (TextBox)ctr;
                                    string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);
                                    r[name] = txtbox.Text;
                                }
                            }
                        }
                        if (ddlExpressCompany.SelectedValue == "2")
                        {
                            txtOrderId.Value =" ";
                            r["OrderId"] = txtOrderId.Value;
                        }
                        r["Muser"] = userId;
                        r["Mtime"] = System.DateTime.Now;
                        r["Auser"] = userId;
                        r["Atime"] = defaultDateTime;
                        int val = adapter.Update(r);
                        if (val > 0)
                        {
                            Response.Write("<script>alert('修改成功!');window.location.href='RealPostage.aspx'</script>");
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    protected void ddlExpressCompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        string val = ddlExpressCompany.SelectedValue.ToString();
        if(val =="2")
        {
            txtOrderId.Value = " ";
            txtOrderId.Attributes["readonly"] = "readonly";        
        }
    }  
}